package com.csx.micrometer.simple.threadpool.config;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author cuisongxu
 * @date 2023/2/7 16:11
 */
@EnableConfigurationProperties(ThreadPoolProperties.class)
@Configuration
public class ThreadPoolMetricsConfiguration {

    @Bean
    public ThreadPoolExecutor threadPoolExecutor(ThreadPoolProperties threadPoolProperties) {

        int corePoolSize = threadPoolProperties.getCorePoolSize();
        int maximumPoolSize = threadPoolProperties.getMaximumPoolSize();
        long keepAliveTime = threadPoolProperties.getKeepAliveTime();
        int queueSize = threadPoolProperties.getQueueSize();

        return new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, TimeUnit.SECONDS, new ArrayBlockingQueue<>(queueSize));
    }
}
